﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;

public partial class Controls_BackEnd_AdvertisementEdit : UserControl
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;
        GetData();
    }


    /// <summary>
    /// 
    /// </summary>
    private void GetData()
    {
        Session[SessionManager.SessionControl] = "~/Controls/Common/FileManager.ascx";
        Bindcategory();
        Advertisement = Config.Id > 0 ? new Adv { AdvID = Config.Id }.AdvGetByAdvId() :
        new Adv();
        CheckPermission();

    }


    /// <summary>
    /// 
    /// </summary>
    private void CheckPermission()
    {

        var permission = (UserFunctions)Session[SessionManager.SessionPermission];
        btnSave.Visible = Config.Id > 0 ? permission.IsUpdate : permission.IsInsert;
        btnReset.Visible = permission.IsInsert;
    }



    private string GetUrlImage()
    {
        try
        {
            var url = txtImage.Text.ToLower();
            var link = Config.ImageUrl.ToLower();
            return url.Replace(link, string.Empty);

        }
        catch { }
        return string.Empty;
    }


    /// <summary>
    /// 
    /// </summary>
    protected Adv Advertisement
    {
        get
        {
            var adv = (Adv)Session[SessionManager.SessionData] ?? new Adv();
            adv.CateID = Config.ConvertToInt(ddlcate.SelectedValue);
            adv.Code = txtCode.Text;
            adv.IsActive = cbActive.Checked;
            adv.Name =txtName.Text;
            adv.Link = txtLink.Text;
            adv.Image = GetUrlImage();
            adv.Description = txtDescript.Text;
            adv.UserName = Session[SessionManager.SessionUserName].ToString();
            return adv;
        }
        set
        {
            Session[SessionManager.SessionData] = value;
            txtName.Text = value.Name;
            cbActive.Checked = value.IsActive;
            txtCode.Text = value.Code;
            txtLink.Text = value.Link;
            txtDescript.Text = value.Description;
            ddlcate.SelectedValue = value.CateID.ToString();
            if (string.IsNullOrEmpty(value.Image)) return;
            txtImage.Text = Config.ImageUrl + value.Image;
            ImgAvarta.ImageUrl = Config.ImageUrl + value.Image;
        }
    }




    /// <summary>
    /// 
    /// </summary>
    private void Bindcategory()
    {
        ddlcate.Items.Clear();
        ddlcate.Items.Add(new ListItem { Value = @"0", Text = @"--- Select category ---" });
        var list = new CategoryAdv().CategoryAdv_GetByCondition(" IsActive=1");
        foreach (var categoriese in list)
            ddlcate.Items.Add(new ListItem { Value = categoriese.CateID.ToString(), Text = categoriese.CateName });

    }




    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AllCommand(object sender, EventArgs e)
    {
        switch (((Control)sender).ID)
        {
            case "btnReset":
                Advertisement = new Adv();
                Config.Id = 0;
                break;
            case "btnBack":
                Config.Back();
                break;
            case "btnSave":
                var error = Advertisement.Save();
                if (error > 0)
                {
                    Advertisement.CateID = Config.Id = error;
                    Config.Message(Page, Resources.Resource.SaveSuccess, Resources.Resource.Messenger);
                    return;
                }
                switch (error)
                {
                    case -1:
                        Config.Message(Page, string.Format(Resources.Resource.ObjectExisted, Resources.Resource.Advertisement), Resources.Resource.Warning);
                        break;
                    default:
                        Config.Message(Page, Resources.Resource.ErrorMsg, Resources.Resource.Warning);
                        break;
                }
                break;

        }

    }


}


